package com.x.windows;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.http.Cookie;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.Json;
import io.vertx.core.net.JksOptions;
import io.vertx.ext.bridge.BridgeEventType;
import io.vertx.ext.bridge.PermittedOptions;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.sockjs.SockJSBridgeOptions;
import io.vertx.ext.web.handler.sockjs.SockJSHandler;

import java.util.ArrayList;
import java.util.List;

public class MainVerticle extends AbstractVerticle {
  List<String> users= new ArrayList<>();
  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    Router router = Router.router(vertx);
    //static
    Route route = router.route("/*");
    route.handler(StaticHandler.create().setMaxAgeSeconds(1).setCacheEntryTimeout(1).setFilesReadOnly(false));

    //id gen
    Route route1 = router.route("/gen-id");
    route1.handler(routingContext -> {
      if (routingContext.request().getCookie("myid") != null){
        if(System.currentTimeMillis()-Long.parseLong(routingContext.request().getCookie("time").getValue()) > 36000000){
          users.remove(routingContext.request().getCookie("myid").getValue());
          String id = IDGEN.creatUUID();
          System.out.println(routingContext.request().getCookie("myid").getValue()+" exit");
          routingContext.response().addCookie(Cookie.cookie("myid",id)).addCookie(Cookie.cookie("time",String.valueOf(System.currentTimeMillis()))).end(id);
          System.out.println(id+" join");
          users.add(id);
          return;
        }else {
          if(!users.contains(routingContext.request().getCookie("myid").getValue())){
            users.add(routingContext.request().getCookie("myid").getValue());
          }
          routingContext.response().end((String) routingContext.request().getCookie("myid").getValue());

          return;
        }
      }
      String id = IDGEN.creatUUID();
      routingContext.addCookie(Cookie.cookie("myid",id)).addCookie(Cookie.cookie("time",String.valueOf(System.currentTimeMillis()))).response().end(id);
      System.out.println(id+" join");
    });
    //sockjs
    Route websocket = router.route("/eventbus/*");
    SockJSHandler handler = SockJSHandler.create(vertx);
    PermittedOptions in1 = new PermittedOptions().setAddressRegex("user\\..+");

    SockJSBridgeOptions options = new SockJSBridgeOptions()
      .addInboundPermitted(in1)
      .addOutboundPermitted(new PermittedOptions().setAddressRegex("user\\..+"));
    websocket.subRouter(handler.bridge(options));

    EventBus bus = vertx.eventBus();
    bus.consumer("user.hello",(message -> {
      users.add((String) message.body());
      System.out.println("user:"+message.body()+" online");
    }));
    bus.consumer("user.exit",(message)->{
      users.remove((String) message.body());
      System.out.println("user:"+message.body()+" exit");
    });

    vertx.createHttpServer()
      .requestHandler(router)
      .listen(1080)

      .onSuccess(httpServer -> {
        System.out.println("finish creating web server(without ssl).port 1080");
      })
      .onFailure(Throwable::printStackTrace);
    vertx.createHttpServer(new HttpServerOptions().setSsl(true).setKeyStoreOptions(new JksOptions().setPath("1.jks").setPassword("ethpqpkk")))
      .requestHandler(router)
      .listen(1443)
      .onSuccess(httpServer -> {
        System.out.println("finish creating web server(with ssl).port 1443");
      })
      .onFailure(Throwable::printStackTrace);
  }
}
